﻿using Learun.Util.SqlSugar;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    public class ec_projectSettingsService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_projectSettingsEntity> _projectSettingsRepository => new Repository<ec_projectSettingsEntity>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        public IEnumerable<ec_projectSettingsEntity> GetList(string projId)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(@"t.*");
                if (string.IsNullOrWhiteSpace(projId))
                {
                    strSql.Append(@"  FROM ec_projectSettings t");
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(projId);
                    strSql.Append($@"  FROM ec_projectSettings_{ProjectEntity.ProjectIndex} t");
                }
                strSql.Append("  WHERE 1=1 ");

                //return this.BaseRepository().FindList<ec_projectSettingsEntity>(strSql.ToString());
                return SqlSugarHelper.Db.SqlQueryable<ec_projectSettingsEntity>(strSql.ToString()).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }


        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(ec_projectSettingsEntity entity, string ProjectId)
        {
            try
            {
                var list = GetList(ProjectId);

                var res = list.FirstOrDefault(x => x.SettingName == entity.SettingName);
                if (res != null)
                {
                    entity.SettingID = res.SettingID;
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _projectSettingsRepository.Update(entity);
                    }
                    else
                    {
                        var ProjectEntity = _projectRepository.GetById(ProjectId);
                        //this.BaseRepository().UpdateByNoMap(entity, $"ec_projectSettings_{ProjectEntity.ProjectIndex}", "SettingID");
                        _projectSettingsRepository.AsUpdateable(entity).AS($"ec_projectSettings_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
                else
                {
                    entity.SettingID = Guid.NewGuid().ToString();
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _projectSettingsRepository.Insert(entity);
                    }
                    else
                    {
                        var ProjectEntity = _projectRepository.GetById(ProjectId);
                        //this.BaseRepository().InsertByNoMap(entity, $"ec_projectSettings_{ProjectEntity.ProjectIndex}");
                        _projectSettingsRepository.AsInsertable(entity).AS($"ec_projectSettings_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }


        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _projectSettingsRepository.Delete(t => t.SettingID == keyValue);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    SqlSugarHelper.Db.Ado.ExecuteCommand($@" DELETE FROM ec_projectSettings_{ProjectEntity.ProjectIndex} WHERE SettingID = '{keyValue}';");
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
    }
}
